#include "stable.h"
#include <QkToolBar.h>

QkToolBar::QkToolBar() : 
	m_buttonGroup(this),
	m_layout(NULL)
{
	this->setMaximumHeight(40);
	connect(&m_buttonGroup, SIGNAL(buttonClicked(QAbstractButton*)), this, SLOT(on_buttonClicked(QAbstractButton*)));
}

QkToolBar::~QkToolBar()
{
}

//void QkToolBar::paintEvent(QPaintEvent *event)
//{
//	Q_UNUSED(event);
//
//	QPainter p(this);
//	p.fillRect(rect(), Qt::red);
//}

void QkToolBar::setActions(const QList<QAction *> &actions)
{
	if(m_layout == NULL)
	{
		m_layout = new QHBoxLayout(this);
		setLayout(m_layout);
	}

	m_actions = actions;

	QList<QAbstractButton *> buttons = m_buttonGroup.buttons();
	foreach(QAbstractButton *button, buttons)
	{
		m_buttonGroup.removeButton(button);
		delete button;
	}

	foreach(QAction *action, actions)
	{
		QPushButton *button = new QPushButton(action->text());
		button->setSizePolicy(QSizePolicy::Preferred, QSizePolicy::Preferred);
		m_buttonGroup.addButton(button);
		m_layout->addWidget(button);
	}
}

void QkToolBar::on_buttonClicked(QAbstractButton *button)
{
	QList<QAbstractButton *> buttonList = m_buttonGroup.buttons();
	int index = buttonList.indexOf(button);
	
	m_actions.at(index)->trigger();
}